package com.jiwei.system.interceptor;

import cn.yiban.open.Authorize;
import cn.yiban.open.FrameUtil;
import com.jiwei.system.pojo.MemberLogin;
import com.jiwei.system.serviceImpl.MemberServiceImpl;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MemberLoginInterceptor implements HandlerInterceptor {
    @Resource(name = "MemberService")
    private MemberServiceImpl memberService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

//        String token = CookieUtil.getCookieValueNamedToken(request);
        MemberLogin memberLogin = (MemberLogin) request.getSession().getAttribute("login");
        String url;
//        FrameUtil util = new FrameUtil(request, response, "36321b8cbd2e6b15", "c484d43c629679689e61716c09d6b58b", "http://f.yiban.cn/iapp675285");
//        boolean reasult = util.perform();
//        System.out.print("token:");
//        System.out.println(util.getAccessToken());
//        if(util.getAccessToken()==null){
//            Authorize au = new Authorize("36321b8cbd2e6b15", "c484d43c629679689e61716c09d6b58b");
//            String urll = au.forwardurl("http://f.yiban.cn/iapp675285", "QUERY", Authorize.DISPLAY_TAG_T.WEB);
//            response.sendRedirect(urll);
//            return false;
//        }
        if (request.getServletPath().equals("/member/login")) {
            if (request.getMethod().equals("POST")){
                return true;
            }
            else if (memberLogin!= null) {
                String role = memberLogin.getRole();
                System.out.println(role);
                if(role.equals("管理员")){
                    url = "/admin/index";
                    response.sendRedirect(url);
                }else if(role.equals("接线员")){
                    url = "/operator/index";
                    response.sendRedirect(url);
                }else{
                    url = "/member/index";
                    response.sendRedirect(url);
                }


                //getRole(response,request);

                return true;
            } else
                return true;
        } else if (memberLogin == null) {
            response.sendRedirect("/member/login");
            return false;
        } else {
            //getRole(response,request);
            return true;
        }


    }

    /*private void getRole(HttpServletResponse response,HttpServletRequest request)  {
        String roleToken = CookieUtil.getCookieValueNamedRoleToken(request);
        if(roleToken==null||!JWTUtil.verifyToken(roleToken)){
            String token = CookieUtil.getCookieValueNamedToken(request);
            String id = JWTUtil.getClaimNamedAccount(token);
            String role = memberService.getMemberRoleByID(id);
            roleToken = JWTUtil.createRoleToken(role);
            CookieUtil.setRoleCookie(response,roleToken);
            CookieUtil.setCookie(response,"time_coo",String.valueOf(System.currentTimeMillis()),"member",-1);

        }
    }*/
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {



    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
